package edu.ycxy.poetry.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.ycxy.poetry.entity.ShiWen;
import edu.ycxy.poetry.entity.Writer;
import edu.ycxy.poetry.vo.AddPoemVo;
import edu.ycxy.poetry.vo.UpdatePoemVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@Repository
public interface ShiWenMapper extends BaseMapper<ShiWen> {

    //根据id升序分页选出前42首古诗
    @Select("select * from vab_shiwen order by id desc limit 0,42")
    List<ShiWen> findByTime();

    //根据作者姓名统计作者的诗词总数
    @Select("select count(*) from vab_shiwen where zuozhe=#{name}")
    Integer findCountForWriter(String name);

    //根据古诗名查询古诗的整首诗
    @Select("select * from vab_shiwen where title=#{poemName} order by id desc limit 0,1")
    ShiWen findPoemByName(String poemName);

    //根据古诗名查询古诗作者的生平
    @Select("select a.zuozhe,b.txt from vab_shiwen a left join vab_writer b on a.zuozhe=b.name where a.title = #{poemName} limit 0,1")
    Writer findWriteByPoemName(String poemName);

    //根据古诗文的上传时间排序对古诗词进行查
    @Select("select * from vab_shiwen order by time")
    List<ShiWen> findAllShiWen();

    //根据古诗的id查询古诗的详细信息
    ShiWen selectById(Integer id);

    //根据作者姓名查询该作者的所有诗词
    @Select("select * from vab_shiwen where zuozhe=#{name}")
    List<ShiWen> selectListByAuthor(String name);

    //根据古诗的类型查询古诗的信息，<唐诗三百首> 等 , 模糊查询，使用CONCAT关键字
    @Select("select * from vab_shiwen where type like CONCAT('%',#{typeName},'%')")
    List<ShiWen> findListByTypeName(String typeName);

    //新增古诗词
    @Insert("insert into vab_shiwen(title,chaodai,body,zuozhe,time,type) values (#{title},#{chaodai},#{body},#{zuozhe},(select UNIX_TIMESTAMP(NOW())),#{type})")
    Integer addPoem(AddPoemVo addPoemVo);

    // 根据古诗内容查询古诗
    @Select("select * from vab_shiwen where body=#{body}")
    ShiWen findOneByBody(String body);

    //对诗词进行模糊查询,匹配多个值,匹配标题和内容
    @Select("select * from vab_shiwen where 1=1 and CONCAT(title,body) like CONCAT('%',#{search},'%')")
    List<ShiWen> findListBySearchToLike(String search);

    // 根据诗词Id删除诗词
    @Delete("delete from vab_shiwen where id=#{id}")
    Integer deletePoemById(Integer id);

    @Update("update vab_shiwen set title=#{title},chaodai=#{chaodai},body=#{body},zuozhe=#{zuozhe},time=(select UNIX_TIMESTAMP(NOW())),type=#{type} where id=#{id}")
    Integer updatePoemById(UpdatePoemVo updatePoemVo);
}
